package com.medistat.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;

import com.medistat.controllers.LoginController;

public class AutheticationFilter extends AbstractAuthenticationProcessingFilter{

	protected AutheticationFilter() {
		super("/login");
	}

	private LoginController loginController;
	
	@Autowired
	public void setLoginController(LoginController loginController) {
		this.loginController = loginController;
	}
	
	@Override
	public Authentication attemptAuthentication(HttpServletRequest arg0, HttpServletResponse arg1) throws AuthenticationException, IOException, ServletException {
		String username = loginController.getUsername();
		String password = loginController.getPassword();
		
		UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
		return this.getAuthenticationManager().authenticate(token);
	}

}
